#include "openingupdateappresponse.h"
#include "newspaper/categorydetail.h"

#include <QDebug>

OpeningUpdateAppResponse::OpeningUpdateAppResponse(QObject *parent) :
    JSONResponse(parent),
    m_checkReplyDataSuccess(false)

{
}

// ---------------------------------------------------------------------------------
Network::Response::Type OpeningUpdateAppResponse::type() const
{
    return Response::OpeningUpdateApp;
}

// ---------------------------------------------------------------------------------
bool OpeningUpdateAppResponse::parseSidsLastUpdate(const QVariantMap &map)
{
    if (!map.contains("default_user_sids")) {
        setError(Response::Property, "The \"status\" value missing");
        return false;
    }
    QVariant defaultUserSids = map.value("default_user_sids");
    if (defaultUserSids.type() != QVariant::List) {
        setError(Response::Property, "The \"status\" value isn't competitived");
        return false;
    }

    QStringList listDefaultUserSids = defaultUserSids.toStringList();

    for (int i = 0; i < listDefaultUserSids.length(); i++) {
        m_listSourceCateInfoIdDefault.append(listDefaultUserSids.at(i).toInt());
    }

    return true;
}

// ---------------------------------------------------------------------------------
bool OpeningUpdateAppResponse::parseDefaultSourcesId(const QVariantMap &map)
{
    if (!map.contains("sids_lastupdate")) {
        setError(Response::Property, "The \"status\" value missing");
        return false;
    }
    QVariant sidsLastUpdate = map.value("sids_lastupdate");
    if (sidsLastUpdate.type() != QVariant::Int) {
        setError(Response::Property, "The \"status\" value isn't competitived");
        return false;
    }

    return true;
}

// ---------------------------------------------------------------------------------
bool OpeningUpdateAppResponse::parseSimageDomain(const QVariantMap &map)
{
    if (!map.contains("simage_domain")) {
        setError(Response::Property, "The \"status\" value missing");
        return false;
    }
    QVariant simageDomain = map.value("simage_domain");
    if (simageDomain.type() != QVariant::String) {
        setError(Response::Property, "The \"status\" value isn't competitived");
        return false;
    }

    m_simageDomain = simageDomain.toString();

    return true;
}

// ---------------------------------------------------------------------------------
bool OpeningUpdateAppResponse::parseDefaultCatesId(const QVariantMap &map)
{
    if (!map.contains("default_cates")) {
        setError(Response::Property, "The \"status\" value missing");
        return false;
    }
    QVariant defaultCatesId = map.value("default_cates");
    if (defaultCatesId.type() != QVariant::List) {
        setError(Response::Property, "The \"status\" value isn't competitived");
        return false;
    }

    QStringList listDefaultCatesId = defaultCatesId.toStringList();

    for (int i = 0; i < listDefaultCatesId.length(); i++) {
        m_listCategoryIDInfoDefault.append(listDefaultCatesId.at(i).toInt());
    }

    return true;
}

// ---------------------------------------------------------------------------------
bool OpeningUpdateAppResponse::parseListCategoryPaper(const QVariantMap &map)
{
    if (!map.contains("cinfos")) {
        setError(Response::Property, "The \"cinfos\" value missing");
        return false;
    }
    QVariant cInfosList = map.value("cinfos");
    if (cInfosList.type() != QVariant::List) {
        setError(Response::Property, "The \"cinfos\" value isn't competitived");
        return false;
    }

    QVariantList listCateInfos = cInfosList.toList();

    foreach (QVariant item, listCateInfos) {

        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The \"map\" field isn't a map"));
            return false;
        }

        QVariantMap mapChild = item.toMap();

        if (!mapChild.contains("cid")) {
            setError(Response::Property, "The \"cid\" value missing");
            return false;
        }
        QVariant cid = mapChild.value("cid");
        if (cid.type() != QVariant::Int) {
            setError(Response::Property, "The \"cid\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("name")) {
            setError(Response::Property, "The \"name\" value missing");
            return false;
        }
        QVariant name = mapChild.value("name");
        if (name.type() != QVariant::String) {
            setError(Response::Property, "The \"name\" value isn't competitived");
            return false;
        }


        CategoryInfo *aCategoryPaper = new CategoryInfo(cid.toInt() , name.toString());

        m_listCategoryPaper.append(aCategoryPaper);
    }

    return true;
}

// ---------------------------------------------------------------------------------
bool OpeningUpdateAppResponse::parseListSourcesInfo(const QVariantMap &map)
{
    if (!map.contains("sinfos")) {
        setError(Response::Property, "The \"sinfos\" value missing");
        return false;
    }
    QVariant sInfosList = map.value("sinfos");
    if (sInfosList.type() != QVariant::List) {
        setError(Response::Property, "The \"sinfos\" value isn't competitived");
        return false;
    }

    QVariantList listSourceInfos = sInfosList.toList();

    foreach (QVariant item, listSourceInfos) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The \"map\" field isn't a map"));
            return false;
        }

        QVariantMap mapChild = item.toMap();

        SourceDetail* sourceDetail = parseASourceDetail(mapChild);
        if(sourceDetail)
        {
            m_mapId2SourceDetail.insert(sourceDetail->sourceId(), sourceDetail);
            m_listSourceDetail.append(sourceDetail);
        }

    }

    return true;
}

void OpeningUpdateAppResponse::parse(const QVariant &data)
{
//    qDebug() << data;
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The error code field isn't competitived");
        return;
    }

    QVariantMap map = data.toMap();

    if (!parseSidsLastUpdate(map)) {
        return;
    }

    if (!parseDefaultSourcesId(map)) {
        return;
    }

    if (!parseSimageDomain(map)) {
        return;
    }

    if (!parseDefaultCatesId(map)) {
        return;
    }

    if (!parseListCategoryPaper(map)) {
        return;
    }

    if (!parseListSourcesInfo(map)) {
        return;
    }


    m_checkReplyDataSuccess = true;
}


// ---------------------------------------------------------------------------------

SourceDetail* OpeningUpdateAppResponse::parseASourceDetail(const QVariantMap &map)
{
    if (!map.contains("sid")) {
        setError(Response::Property, "The \"sid\" value missing");
        return NULL;
    }
    QVariant sid = map.value("sid");
    if (sid.type() != QVariant::Int) {
        setError(Response::Property, "The \"sid\" value isn't competitived");
        return NULL;
    }

    if (!map.contains("name")) {
        setError(Response::Property, "The \"name\" value missing");
        return NULL;
    }
    QVariant name = map.value("name");
    if (name.type() != QVariant::String) {
        setError(Response::Property, "The \"name\" value isn't competitived");
        return NULL;
    }

    if (!map.contains("url")) {
        setError(Response::Property, "The \"url\" value missing");
        return NULL;
    }
    QVariant url = map.value("url");
    if (url.type() != QVariant::String) {
        setError(Response::Property, "The \"url\" value isn't competitived");
        return NULL;
    }

    if (!map.contains("cids")) {
        setError(Response::Property, "The \"cids\" value missing");
        return NULL;
    }
    QVariant listCids = map.value("cids");
    if (listCids.type() != QVariant::List) {
        setError(Response::Property, "The \"status\" value isn't competitived");
        return NULL;
    }

    QList < CategoryDetail* > listCategoryPaper;
    QMap< int, CategoryDetail*> mapCategoryDetail;

    QStringList sListCids = listCids.toStringList();

    if (sListCids.length() != 0) {
        for (int i = 0; i < sListCids.length(); i++) {
            int aCid = sListCids.at(i).toInt();
            CategoryDetail* categoryDetail = new CategoryDetail(aCid,sid.toInt());
            listCategoryPaper.append(categoryDetail);
            mapCategoryDetail[categoryDetail->idCategory()] = categoryDetail;

        }
    } else {
        // getdata default
        for(int i = 0; i< m_listSourceCateInfoIdDefault.length(); ++i)
        {
            int aCid = m_listSourceCateInfoIdDefault.at(i);
            CategoryDetail* categoryDetail = new CategoryDetail(aCid, sid.toInt());
            listCategoryPaper.append(categoryDetail);
            mapCategoryDetail[categoryDetail->idCategory()] = categoryDetail;
        }
    }

    SourceDetail* sourceD  =new SourceDetail(sid.toInt(),name.toString(), QUrl(url.toString()), QUrl(m_simageDomain), listCategoryPaper, mapCategoryDetail);

    return sourceD;
}

// ---------------------------------------------------------------------------------

